package controllers

import (
	"github.com/gin-gonic/gin"
	"github.com/tommy351/gin-sessions"
	"net/http"
	"strconv"
	"time"
	"wallet/models"
)

func Home(c *gin.Context) {
	s := sessions.Get(c)
	user := s.Get("current_user")
	value, ok := user.(*models.User)
	if !ok {
		return
	}
	books, _ := models.SelectBookListByUserID(value.ID)
	c.HTML(http.StatusOK, "home.html", gin.H{
		"books": books,
		"user":  value,
	})
}

func Index(c *gin.Context) {
	//session := sessions.Get(c)
	start := c.Query("start")
	end := c.Query("end")
	bookId := c.Query("bookid")
	month := c.Query("month")
	if len(month) == 0 {
		month = time.Now().Format("2006-01")
	}
	_bookId, _ := strconv.ParseInt(bookId, 10, 8)

	common, per, results := Calculate(uint(_bookId), month, start, end)

	c.HTML(http.StatusOK, "index.html", gin.H{
		"common": common,
		"per":    per,
		"data":   results,
	})
}
